import 'dart:ffi';

import 'package:schoollifedemo/channel/channel_name.dart';
import 'package:schoollifedemo/channel/register/channel_register.dart';

// 此处包含了CommonChannel中的方法声明
class CommonChannelFunc {
  final ChannelRegister _channelRegister;

  CommonChannelFunc(this._channelRegister);

  Future<void> photoSelector() async {
    _channelRegister.commonChannel?.invokeMethod(CommonChannel.photoSelector);
  }

  Future<void> showAlertDialog({String? title, String? msg}) async {
    _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.showAlertDialog, {
      'title': title,
      'msg': msg,
    });
  }

  // 获取app版本
  Future<String?> getAppVersion() async {
    return _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.getAppVersion, {'version': 'version'});
  }

  // 设置app版本号
  Future<void> setAppVersion(String version) async {
    _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.setAppVersion, {'version': version});
  }

  // 转换为sha256
  Future<String?> convertSHA256(String object) async {
    return await _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.convertSHA256, {'object': object});
  }

  // 转换为md5
  Future<String?> convertMD5(String object) async {
    return await _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.convertMD5, {'object': object});
  }

  // 编码为base64
  Future<String?> encodeBase64(String? object) async {
    return await _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.encodeBase64, {'object': object});
  }

  // base64解码
  Future<String?> decodeBase64(String? object) async {
    return await _channelRegister.commonChannel
        ?.invokeMethod(CommonChannel.decodeBase64, {'object': object});
  }

  // 保存String类型到SharedPreference中
  Future<void> saveToSharedPreferenceString(String key, String value) async {
    _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.saveToSharedPreferenceString,
        {'key': key, 'value': value});
  }

  // 保存Bool类型到SharedPreference中
  Future<void> saveToSharedPreferenceBool(String key, bool value) async {
    _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.saveToSharedPreferenceBool, {'key': key, 'value': value});
  }

  Future<String?> getFromSharedPreferenceString(
      String key, String defaultValue) async {
    return await _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.getFromSharedPreferenceString,
        {'key': key, 'defaultValue': defaultValue});
  }

  Future<bool?> getFromSharedPreferenceBool(
      String key, bool defaultValue) async {
    return await _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.getFromSharedPreferenceBool,
        {'key': key, 'defaultValue': defaultValue});
  }

  Future<int?> getFromSharedPreferenceInt(String key, int defaultValue) async {
    return await _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.getFromSharedPreferenceInt,
        {'key': key, 'defaultValue': defaultValue});
  }

  Future<Float?> getFromSharedPreferenceFloat(
      String key, Float defaultValue) async {
    return await _channelRegister.commonChannel?.invokeMethod(
        CommonChannel.getFromSharedPreferenceFloat,
        {'key': key, 'defaultValue': defaultValue});
  }
}
